package com.notebad.ui;

import java.io.File;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.notebad.R;
import com.notebad.note.Note;

/**
 * Liste des sons associes a une note
 * @author Ludo
 */
public class SoundsListView extends ListActivity {

	private static final String TAG = "[SoundsListView]";
	private static final int AUDIO_ID = 0;
	private ArrayAdapter<String> adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		
		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				//Lancer une Activity qui fasse player
				String[] sounds = Note.getCurrentNote().getSounds();
				File tmp = new File(sounds[position]);
				if(tmp.exists())
				{
					Intent player = new Intent(SoundsListView.this,AudioPlayerView.class);
					player.putExtra("soundPath", sounds[position]);
					startActivity(player);
				}
			}
		});
		
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int index, long arg3) {
				AlertDialog.Builder builder = new AlertDialog.Builder(SoundsListView.this);
				builder.setMessage("Supprimer cet enregistrement ?")
				       .setCancelable(false)
				       .setPositiveButton("Oui", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				        	   //Supprimer ici le fichier correspondant a cet enregistrement
				        	   String[] sounds = Note.getCurrentNote().getSounds();
								File tmp = new File(sounds[index]);
								if(tmp.exists()){
									tmp.delete();
								}
								onResume();
				           }
				       })
				       .setNegativeButton("Non", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
				return false;
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");	
		adapter = new ArrayAdapter<String>(this, R.layout.sounds_list, Note.getCurrentNote().getSounds());
		setListAdapter(adapter);
		if(Note.getCurrentNote().getSounds().length == 0){
			Toast.makeText(getBaseContext(), "Aucun enregistrement pour cette note", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, AUDIO_ID, 0, R.string.SoundsListView_menu_addSound).setIcon(R.drawable.ic_menu_micro);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		case AUDIO_ID:
			Log.d(TAG, "Menu - add sound");
			Intent recorder = new Intent(this,AudioRecorderView.class);
			startActivity(recorder);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
}
